package com.mtf.controller;

import com.alibaba.fastjson.JSON;
import com.mtf.common.vo.Result;
import com.mtf.entity.MtfOrder;
import com.mtf.service.IMtfOrderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.util.UUID;

import static com.mtf.common.enums.IdempotentEnum.*;

/**
 * @Description: mtf_order
 * @Author: jeecg-boot
 * @Date: 2023-11-02
 * @Version: V1.0
 */
@Api(tags = "mtf_order")
@RestController
@RequestMapping("/order")
@Slf4j
public class MtfOrderController {
    @Autowired
    private IMtfOrderService mtfOrderService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/test")
    public String test(){
        return "测试接口！";
    }

    /**
     * 创建订单
     *
     * @param mtfOrder
     * @return
     */
    @ApiOperation(value = "mtf_order-添加", notes = "mtf_order-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody MtfOrder mtfOrder) {
        //设置订单标识

        //重复调用创建订单接口，设置相同订单唯一标识，模拟消费者消费重复消息
        //mtfOrder.setIdentity(UUID.randomUUID().toString());
        //mtfOrder.setIdentity("deb0eb49-4b67-42df-948f-ede13be7a21a");

        //模拟前端传递相同时间戳重复创建订单，模拟重复消费场景
        //mtfOrder.setTimestamp(String.valueOf(System.currentTimeMillis()));
        mtfOrder.setTimestamp("1710728210549");
        //return mtfOrderService.saveOrder(mtfOrder,SERVICE_IDEMPOTENT);
        //return mtfOrderService.saveOrder(mtfOrder,SERVICE_SELECT_IDEMPOTENT);
        return mtfOrderService.saveOrder(mtfOrder,MESSAGE_TABLE_IDEMPOTENT);
    }

    /**
     * 使用Mq创建订单
     *
     * @param mtfOrder
     * @return
     */
    @ApiOperation(value = "mtf_order-添加", notes = "mtf_order-添加")
    @PostMapping(value = "/addOrderByMq")
    public Result<?> addOrderByMq(@RequestBody MtfOrder mtfOrder) {
        //mtfOrder.setIdentity(UUID.randomUUID().toString());
        //设置固定标识，重复请求接口模拟重复消费场景
        mtfOrder.setIdentity("c076d895-1447-43c9-a270-fcc3a3a9db9b");
        Message rocketMessage = new Message();
        rocketMessage.setBody(JSON.toJSONBytes(mtfOrder));
        rocketMessage.setTopic("idempotent_topic");
        //普通消息：sendOneWay(单向消息)、sendOneWayOrderly(单向顺序消息)、asyncSend(异步消息)、asyncSendOrderly(异步顺序消息)、syncSend(同步消息)、syncSendOrderly(同步顺序消息)
        //事务消息：sendMessageInTransaction
        rocketMQTemplate.syncSend("idempotent_topic",rocketMessage);
        //rocketMQTemplate.send("idempotent_topic",rocketMessage);
        return Result.ok("订单创建成功！");
    }

    /**
     * 订单详情接口（订单id查询）
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "mtf_order-详情", notes = "mtf_order-详情")
    @GetMapping(value = "/getInfo")
    public MtfOrder getInfo(@RequestParam("id") String id) {
        return mtfOrderService.getInfo(id);
    }
    /**
     * 订单详情接口（订单identity查询）
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "mtf_order-详情", notes = "mtf_order-详情")
    @GetMapping(value = "/getInfos")
    public MtfOrder getInfos(@RequestParam("id") String id) {
        return mtfOrderService.getInfos(id);
    }

}
